<template>
  <el-dialog :visible.sync="visible" :title="$t('user.secSetFrom')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="secForm" :rules="dataRule" ref="secForm" @keyup.enter.native="secFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'">
      <el-form-item prop="username" :label="$t('user.username')">
        <el-input type="label" v-model="secForm.username" :placeholder="$t('user.username')" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item prop="secRoleIdList" :label="$t('user.secMgrRole')" class="role-list">
        <el-select v-model="secForm.secRoleIdList" multiple :placeholder="$t('user.secMgrRole')">
          <el-option v-for="role in secRoleList" :key="role.id" :label="role.name" :value="role.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="secFormSubmitHandle('0')">{{ $t('user.secAuthBack') }}</el-button>
      <el-button type="primary" @click="secFormSubmitHandle('2')">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      secRoleList: [],
      secForm: {
        id: '',
        username: '',
        deptId: '0',
        deptName: '',
        password: '',
        comfirmPassword: '',
        realName: '',
        gender: 0,
        email: '',
        mobile: '',
        roleIdList: [],
        secRoleIdList: [],
        status: 1
      }
    }
  },
  computed: {
    dataRule () {
      return {
        secRoleIdList: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        username: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['secForm'].resetFields()
        Promise.all([
          this.getRoleList()
        ]).then(() => {
          if (this.secForm.id) {
            this.getInfo()
          }
        })
      })
    },
    // 获取角色列表
    getRoleList () {
      return this.$http.get('/sys/role/roleList', { params: { roleGrade: '2' } }).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.secRoleList = res.data
      }).catch(() => { })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/sys/user/${this.secForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.secForm = {
          ...this.secForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    secFormSubmitHandle: debounce(function (flag) {
      this.$confirm(this.$t('prompt.info', { 'handle': flag === '2' ? this.$t('user.secSetFrom') : this.$t('user.secAuthBack') }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$refs['secForm'].validate((valid) => {
          if (!valid) {
            if (flag === '2') {
              return false
            }
          }
          this.$http.post('/sys/user/secAdmin', {
            ...this.secForm,
            secAdmin: flag
          }).then(({ data: res }) => {
            if (res.code !== 0) {
              return
            }
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }).catch(() => { })
        })
      }).catch(() => { })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style lang="scss">
.mod-sys__user {
  .role-list {
    .el-select {
      width: 100%;
    }
  }
}
</style>
